// import './lib-tdt/index.js';

import {
  Credit,
  ProviderViewModel,
  WebMapTileServiceImageryProvider,
  UrlTemplateImageryProvider,
  WebMercatorTilingScheme
} from '@cesium/Source/Cesium.js';

const tk = '0636865296f6329ada1132448e161ae6';
const tdtUrl = 'https://t{s}.tianditu.gov.cn';
const subdomains = ['0', '1', '2', '3', '4', '5', '6', '7'];
const optWebMapTileServiceImageryProviderDefault = {
  layer: 'img',
  style: 'default',
  format: 'tiles',
  // tileMatrixSetID: "GoogleMapsCompatible",//使用谷歌的瓦片切片方式
  tileMatrixSetID: 'w',
  category: '天地图',
  credit: new Credit('天地图全球影像服务'),
  // maximumLevel:7,
  subdomains,
  show: false
};

function imgTileServiceImageryProviderCreator() {
  var esri = new WebMapTileServiceImageryProvider({
    ...optWebMapTileServiceImageryProviderDefault,
    layer: 'tdtImageLayer',
    url: `${tdtUrl}/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`
  });
  return esri;
}

//底图切换
var img_tdt_yx = () => new ProviderViewModel({
  name: '影像底图',
  tooltip: '影像底图',
  //显示切换的图标
  iconUrl: 'http://lbs.tianditu.gov.cn/images/img_c.png',
  creationFunction: imgTileServiceImageryProviderCreator
});

function vecTileServiceImageryProviderCreator() {
  var esri = new WebMapTileServiceImageryProvider({
    ...optWebMapTileServiceImageryProviderDefault,
    layer: 'tdtVectorLayer',
    url: `${tdtUrl}/vec_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`
  });
  return esri;
}
var img_tdt_sl = () => new ProviderViewModel({
  name: '矢量底图',
  tooltip: '矢量底图',
  iconUrl: 'http://lbs.tianditu.gov.cn/images/vec_c.png',
  creationFunction: vecTileServiceImageryProviderCreator
});

function terTileServiceImageryProviderCreator() {
  var esri = new WebMapTileServiceImageryProvider({
    ...optWebMapTileServiceImageryProviderDefault,
    layer: 'tdtTerrainLayer',
    url: `${tdtUrl}/ter_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=ter&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`
  });
  return esri;
}
var img_tdt_dx = () => new ProviderViewModel({
  name: '地形底图',
  tooltip: '地形底图',
  iconUrl: 'http://lbs.tianditu.gov.cn/images/ter_c.png',
  creationFunction: terTileServiceImageryProviderCreator
});

function ciaTileServiceImageryProviderCreator() {
  var esri = new WebMapTileServiceImageryProvider({
    ...optWebMapTileServiceImageryProviderDefault,
    url: `${tdtUrl}/cia_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cia&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`,
    layer: 'tdtImgAnnoLayer',
    // style: 'default',
    // format: 'image/jpeg',
  });
  return esri;
}

// viewer.imageryLayers.addImageryProvider(
//   new Cesium.WebMapTileServiceImageryProvider({
//     url: `${tdtUrl}/cia_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cia&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`,
//     layer: 'tdtImgAnnoLayer',
//     style: 'default',
//     format: 'image/jpeg',
//     tileMatrixSetID: 'GoogleMapsCompatible',
//     show: false
//   })
// );

var img_tdt_anno = () => new ProviderViewModel({
  name: '地形底图',
  tooltip: '地形底图',
  iconUrl: 'http://lbs.tianditu.gov.cn/images/ter_c.png',
  creationFunction: ciaTileServiceImageryProviderCreator
});

//影像底图加载
let imageryProvider = () => new WebMapTileServiceImageryProvider({
  ...optWebMapTileServiceImageryProviderDefault,
  url: `${tdtUrl}/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=${tk}`,
  // layer: 'img',
  // style: 'default',
  // format: 'tiles',
  // tileMatrixSetID: 'w',
  // credit: new Cesium.Credit('天地图全球影像服务'),
  // maximumLevel:7,
  show: false
});


// 叠加国界服务
let iboMapProvider = () => new UrlTemplateImageryProvider({
  ...optWebMapTileServiceImageryProviderDefault,
  url: `${tdtUrl}/DataServer?T=ibo_w&x={x}&y={y}&l={z}&tk=${tk}`,
  tilingScheme: new WebMercatorTilingScheme(),
  maximumLevel: 10,
  show: true
});
function iboServiceImageryProviderCreator() {
  var esri = iboMapProvider();
  return esri;
}
var img_tdt_ibo = () => new ProviderViewModel({
  name: '国界',
  tooltip: '国界',
  iconUrl: 'http://lbs.tianditu.gov.cn/images/ter_c.png',
  creationFunction: iboServiceImageryProviderCreator
});


/*
// 叠加地形服务
var terrainUrls = new Array();
for (var i = 0; i < subdomains.length; i++) {
  var url = tdtUrl.replace('{s}', subdomains[i]) + 'DataServer?T=elv_c&tk=' + tk;
  terrainUrls.push(url);
}
let terrainProvider = new GeoTerrainProvider({
  urls: terrainUrls
});


// 叠加三维地名服务
function addAnnotation3d(viewer) {
  var wtfs = new Cesium.GeoWTFS({
    viewer,
    subdomains: subdomains,
    metadata: {
      boundBox: {
        minX: -180,
        minY: -90,
        maxX: 180,
        maxY: 90
      },
      minLevel: 1,
      maxLevel: 20
    },
    aotuCollide: true, //是否开启避让
    collisionPadding: [5, 10, 8, 5], //开启避让时，标注碰撞增加内边距，上、右、下、左
    serverFirstStyle: true, //服务端样式优先
    labelGraphics: {
      font: "28px sans-serif",
      fontSize: 28,
      fillColor: Cesium.Color.WHITE,
      scale: 0.5,
      outlineColor: Cesium.Color.BLACK,
      outlineWidth: 5,
      style: Cesium.LabelStyle.FILL_AND_OUTLINE,
      showBackground: false,
      backgroundColor: Cesium.Color.RED,
      backgroundPadding: new Cesium.Cartesian2(10, 10),
      horizontalOrigin: Cesium.HorizontalOrigin.MIDDLE,
      verticalOrigin: Cesium.VerticalOrigin.TOP,
      eyeOffset: Cesium.Cartesian3.ZERO,
      pixelOffset: new Cesium.Cartesian2(0, 8)
    },
    billboardGraphics: {
      horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
      verticalOrigin: Cesium.VerticalOrigin.CENTER,
      eyeOffset: Cesium.Cartesian3.ZERO,
      pixelOffset: Cesium.Cartesian2.ZERO,
      alignedAxis: Cesium.Cartesian3.ZERO,
      color: Cesium.Color.WHITE,
      rotation: 0,
      scale: 1,
      width: 18,
      height: 18
    }
  });

  //三维地名服务，使用wtfs服务
  wtfs.getTileUrl = function() {
    return tdtUrl + 'mapservice/GetTiles?lxys={z},{x},{y}&tk=' + token;
  }

  wtfs.getIcoUrl = function() {
    return tdtUrl + 'mapservice/GetIcon?id={id}&tk=' + token;
  }

  wtfs.initTDT([{
    "x": 6,
    "y": 1,
    "level": 2,
    "boundBox": {
      "minX": 90,
      "minY": 0,
      "maxX": 135,
      "maxY": 45
    }
  }, {
    "x": 7,
    "y": 1,
    "level": 2,
    "boundBox": {
      "minX": 135,
      "minY": 0,
      "maxX": 180,
      "maxY": 45
    }
  }, {
    "x": 6,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": 90,
      "minY": 45,
      "maxX": 135,
      "maxY": 90
    }
  }, {
    "x": 7,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": 135,
      "minY": 45,
      "maxX": 180,
      "maxY": 90
    }
  }, {
    "x": 5,
    "y": 1,
    "level": 2,
    "boundBox": {
      "minX": 45,
      "minY": 0,
      "maxX": 90,
      "maxY": 45
    }
  }, {
    "x": 4,
    "y": 1,
    "level": 2,
    "boundBox": {
      "minX": 0,
      "minY": 0,
      "maxX": 45,
      "maxY": 45
    }
  }, {
    "x": 5,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": 45,
      "minY": 45,
      "maxX": 90,
      "maxY": 90
    }
  }, {
    "x": 4,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": 0,
      "minY": 45,
      "maxX": 45,
      "maxY": 90
    }
  }, {
    "x": 6,
    "y": 2,
    "level": 2,
    "boundBox": {
      "minX": 90,
      "minY": -45,
      "maxX": 135,
      "maxY": 0
    }
  }, {
    "x": 6,
    "y": 3,
    "level": 2,
    "boundBox": {
      "minX": 90,
      "minY": -90,
      "maxX": 135,
      "maxY": -45
    }
  }, {
    "x": 7,
    "y": 2,
    "level": 2,
    "boundBox": {
      "minX": 135,
      "minY": -45,
      "maxX": 180,
      "maxY": 0
    }
  }, {
    "x": 5,
    "y": 2,
    "level": 2,
    "boundBox": {
      "minX": 45,
      "minY": -45,
      "maxX": 90,
      "maxY": 0
    }
  }, {
    "x": 4,
    "y": 2,
    "level": 2,
    "boundBox": {
      "minX": 0,
      "minY": -45,
      "maxX": 45,
      "maxY": 0
    }
  }, {
    "x": 3,
    "y": 1,
    "level": 2,
    "boundBox": {
      "minX": -45,
      "minY": 0,
      "maxX": 0,
      "maxY": 45
    }
  }, {
    "x": 3,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": -45,
      "minY": 45,
      "maxX": 0,
      "maxY": 90
    }
  }, {
    "x": 2,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": -90,
      "minY": 45,
      "maxX": -45,
      "maxY": 90
    }
  }, {
    "x": 0,
    "y": 1,
    "level": 2,
    "boundBox": {
      "minX": -180,
      "minY": 0,
      "maxX": -135,
      "maxY": 45
    }
  }, {
    "x": 1,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": -135,
      "minY": 45,
      "maxX": -90,
      "maxY": 90
    }
  }, {
    "x": 0,
    "y": 0,
    "level": 2,
    "boundBox": {
      "minX": -180,
      "minY": 45,
      "maxX": -135,
      "maxY": 90
    }
  }]);
}
*/

export {
  imgTileServiceImageryProviderCreator,
  vecTileServiceImageryProviderCreator,
  terTileServiceImageryProviderCreator,
  ciaTileServiceImageryProviderCreator,
  iboServiceImageryProviderCreator,
  img_tdt_yx,
  img_tdt_sl,
  img_tdt_dx,
  img_tdt_anno,
  img_tdt_ibo,
  imageryProvider,
  // terrainProvider,
  iboMapProvider,
  // addAnnotation3d
}
